package com.tinycloud.miracll.product.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.tinycloud.miracll.product.domain.ScadaNode;
import com.tinycloud.miracll.product.domain.ScadaSetTagValue;
import com.tinycloud.miracll.product.domain.ScadaSetTagValueRequest;
import com.tinycloud.miracll.product.domain.ScadaTagLogValueRequest;
import com.tinycloud.miracll.product.domain.json.ScadaTagLogValues;
import com.tinycloud.miracll.product.domain.json.ScadaTagValues;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;


public  class ScadaUtil {

    static String urlAddr = "http://172.16.203.13/WaWebService/Json/GetTagValue/Miracll-WA";
    static String urlAddrGetDataLog = "http://172.16.203.13/WaWebService/Json/GetDataLog/Miracll-WA";
    static String urlAddrSetTagValue = "http://172.16.203.13/WaWebService/Json/SetTagValue/Miracll-WA";
    static String authUser = "admin";
    static String authPass = "MRada!";

    public  static  List<ScadaTagValues>  getScadaTagValuesList(List<String> tagNames) {
        BasicAuthUtil basicAuthUtil = new BasicAuthUtil();
        List<ScadaNode> scadaNodeList = new ArrayList<>();
        ScadaTagLogValueRequest scadaTagValueRequest = new ScadaTagLogValueRequest();
        for (String tagName:tagNames) {
            ScadaNode scadaNode = new ScadaNode();
            scadaNode.setName(tagName);
            scadaNodeList.add(scadaNode);
        }
        scadaTagValueRequest.setTags(scadaNodeList);
        String msg = JSONObject.toJSONString(scadaTagValueRequest);
        ResponseEntity<String> responseEntity = basicAuthUtil.getHttpResultPost(urlAddr, msg,authUser, authPass);
        String stringResult = responseEntity.getBody();
        System.out.println(stringResult);
        JSONObject jsonResult = JSONObject.parseObject(stringResult);
        JSONArray jsonArray = jsonResult.getJSONArray("Values");
        List<ScadaTagValues> scadaTagValues = JSON.parseArray(jsonArray.toJSONString(),ScadaTagValues.class);
        return  scadaTagValues;
    }

    public  static ScadaTagValues getScadaVaules(String tagName) {
        List<String> tagNames = new ArrayList<String>();
        tagNames.add(tagName);
        List<ScadaTagValues> scadaNodeList  = getScadaTagValuesList(tagNames);
        return  scadaNodeList.get(0);

    }

    public static List<ScadaTagLogValues> getScadaTagValueLog(List<String> tagNames,String beginTime, int timeSpan,int acount) {
        BasicAuthUtil basicAuthUtil = new BasicAuthUtil();
        List<ScadaNode> scadaNodeList = new ArrayList<>();
        ScadaTagLogValueRequest scadaTagLogValueRequest  = new ScadaTagLogValueRequest();
        for (String tagName:tagNames) {
            ScadaNode scadaNode = new ScadaNode();
            scadaNode.setName(tagName);
            scadaNodeList.add(scadaNode);
        }
        scadaTagLogValueRequest.setTags(scadaNodeList);
        scadaTagLogValueRequest.setStartTime(beginTime);
        scadaTagLogValueRequest.setRecords(acount);
        scadaTagLogValueRequest.setIntervalType("S");
        scadaTagLogValueRequest.setInterval(5);
        String msg = JSONObject.toJSONString(scadaTagLogValueRequest);
        System.out.println(msg);
        ResponseEntity<String> responseEntity = basicAuthUtil.getHttpResultPost(urlAddrGetDataLog, msg,authUser, authPass);
        String stringResult = responseEntity.getBody();
//        System.out.println(stringResult);
        JSONObject jsonResult = JSONObject.parseObject(stringResult);
        JSONArray jsonArray = jsonResult.getJSONArray("DataLog");
//        System.out.println(jsonArray.toJSONString());
        List<ScadaTagLogValues> scadaTagLogValues = JSON.parseArray(jsonArray.toJSONString(), ScadaTagLogValues.class);
//        System.out.println(scadaTagLogValues.size());
        return  scadaTagLogValues;

    }

    public static int setScadaTagValue(List<ScadaSetTagValue>  scadaSetTagValueList) {
        BasicAuthUtil basicAuthUtil = new BasicAuthUtil();
        ScadaSetTagValueRequest scadaSetTagValueRequest = new ScadaSetTagValueRequest();
        scadaSetTagValueRequest.setTags(scadaSetTagValueList);

        String msg = JSONObject.toJSONString(scadaSetTagValueRequest);
        System.out.println(msg);
        ResponseEntity<String> responseEntity = basicAuthUtil.getHttpResultPost(urlAddrSetTagValue, msg,authUser, authPass);
        String stringResult = responseEntity.getBody();
        System.out.println(stringResult);
        return  0;
    }


}
